import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
import os
import sys
sys.path.append(os.getcwd())
from config.env import Env
env = Env()

print(cv.useOptimized())
e1 = cv.getTickCount()
# numpy 是模运算，opencv 是饱和运算
x = np.uint8([250])
y = np.uint8([10])
print(cv.add(x, y))
print(x + y)

img1 = cv.imread(env.getImgPath() + 'meinv1.png')
img2 = cv.imread(env.getImgPath() + 'meinv2.jpg')
img3 = cv.imread(env.getImgPath() + 'yunzhihui.png')
img4 = cv.imread(env.getImgPath() + 'back.png')

print(img1.shape)
print(img2.shape)
print(img3.shape)
rows, cols , channels = img3.shape

# img1[0: rows, 0: cols] 是 x坐标 0-rows, y坐标 0-cols 范围内的图像
roi = img1[0: rows, 0: cols]

img3gray = cv.cvtColor(img3, cv.COLOR_BGR2GRAY)
ret, mask = cv.threshold(img3gray, 10, 255, cv.THRESH_BINARY)
mask_inv = cv.bitwise_not(mask)

img1_bg = cv.bitwise_and(roi, roi, mask=mask_inv)
img3_fg = cv.bitwise_and(img3, img3, mask=mask_inv)

# dst = cv.addWeighted(img2, 0.4, img4, 0.9, 0)
dst = cv.add(img1_bg, img3_fg)
img1[0: rows, 0:cols] = dst

e2 = cv.getTickCount()
time = (e2 - e1) / cv.getTickFrequency()
print(time)

e3 = cv.getTickCount()
res = cv.medianBlur(img1, 49)
e4 = cv.getTickCount()

print((e4 - e3) / cv.getTickFrequency())

cv.setUseOptimized(False)
e3 = cv.getTickCount()
res = cv.medianBlur(img1, 49)
e4 = cv.getTickCount()

print((e4 - e3) / cv.getTickFrequency())

cv.imshow("img", img1)
cv.waitKey(0)
cv.destroyAllWindows()